//
//  LLShopGoodsCCell.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLShopGoodsCCell.h"
#import "LLShopGoodsModel.h"

@interface LLShopGoodsCCell ()
@property (strong, nonatomic) UIImageView *imgView;
@property (strong, nonatomic) UILabel *titleLbl;
@property (strong, nonatomic) UILabel *priceLbl;
@property (strong, nonatomic) UILabel *buyCountLbl;
@property (strong, nonatomic) LLButton *shopBtn;
@end

@implementation LLShopGoodsCCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    UIView *bgView = [UIView new];
    bgView.frame = self.bounds;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 5;
    bgView.clipsToBounds = YES;
    [self.contentView addSubview:bgView];
    
    _imgView = UIImageView.new;
    _imgView.frame = CGRectMake(0, 0, self.width, self.width*230/174);
    _imgView.backgroundColor = LLColorRGB(210);
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    _imgView.clipsToBounds = YES;
    [bgView addSubview:_imgView];
    
    _titleLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:13 textAlignment:NSTextAlignmentLeft];
    _titleLbl.frame = CGRectMake(5, _imgView.bottom+10, self.width-10, 20);
    [bgView addSubview:_titleLbl];
    
    _priceLbl = [LLInitBaseViewTool labelWithText:nil textColor:nil font:0 textAlignment:NSTextAlignmentLeft];
    _priceLbl.frame = CGRectMake(_titleLbl.left, _titleLbl.bottom, 0, 20);
    [bgView addSubview:_priceLbl];
    
    _buyCountLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(148) font:10 textAlignment:NSTextAlignmentLeft];
    [bgView addSubview:_buyCountLbl];
    
    _shopBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"shoppingCart_red" titleColor:nil font:0];
    _shopBtn.frame = CGRectMake(self.width-5-20, _priceLbl.top, 20, 20);
    [_shopBtn addTarget:self action:@selector(shopBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_shopBtn];
}

- (void)setModel:(LLShopGoodsModel *)model {
    _model = model;
    [_imgView sd_setImageWithURL:LLImageWithUrl(model.goods_pic) placeholderImage:LLGoodsPlaceholderImage options:SDWebImageRefreshCached];
    _titleLbl.text = model.goods_name;
    NSAttributedString *priceAttr = [NSAttributedString attributedStringsWithFrontText:@"¥" frontFont:13 frontColor:LLColor(231, 11, 11) backText:model.goods_point backFont:19 backColor:LLColor(231, 11, 11)];
    _priceLbl.attributedText = priceAttr;
    CGFloat priceW = [priceAttr boundingRectWithSize:CGSizeMake(self.width-10, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.width;
    _priceLbl.width = priceW;
    _buyCountLbl.text = [NSString stringWithFormat:@"%@人付款",model.goods_point];
    _buyCountLbl.frame = CGRectMake(_priceLbl.right+5, _priceLbl.bottom-15, _shopBtn.left-_priceLbl.right-5-5, 15);
}

- (void)shopBtnClick:(LLButton *)btn {
    if ([self.delegate respondsToSelector:@selector(ccellDidClickShoppingCart:)]) {
        [self.delegate ccellDidClickShoppingCart:self];
    }
}

@end
